from exts import db
from datetime import datetime

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    qq_number = db.Column(db.String(11), nullable=False)
    username = db.Column(db.String(50), nullable=False)
    password = db.Column(db.String(100), nullable=False)

class Conversation(db.Model):
    __tablename__ = 'conversation'
    id = db.Column(db.Integer, primary_key = True, autoincrement = True)
    title = db.Column(db.String(100), nullable = False)
    content = db.Column(db.Text, nullable = False)
    create_time = db.Column(db.DateTime, default = datetime.now)
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    author = db.relationship('User', backref = db.backref('conversations'))

class Comment(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.Integer, primary_key = True, autoincrement=True)
    content = db.Column(db.Text, nullable = False)
    conversation_id = db.Column(db.Integer, db.ForeignKey('conversation.id'))
    create_time = db.Column(db.DateTime, default = datetime.now)
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    conversation = db.relationship('Conversation',backref=db.backref('comments', order_by = id.desc()))
    author = db.relationship('User',backref = db.backref('comments'))

